/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="MItem.js" />


function ChoiceItem(node, basis) {
    ChoiceItem.initializeBase(this, [node, basis]);
    if (this.getMode() == MItem.EditMode) {
        $(this.getSelect()).bind("change", null, Function.createDelegate(this, this.raiseChanged));
        $(this.getSelect()).bind("change", null, Function.createDelegate(this, this.raiseUsageChanged));
    }
    this.addChanged(Function.createDelegate(this, this.validateSelect));
    var select = this.getSelect();
    this.selectedValue = (select != null) ? select.value : null;
}

ChoiceItem.prototype = {
    load: function() {
        ChoiceItem.callBaseMethod(this, 'load');        
    },

    getSelect: function() {
        if (!this.selectNode) {
            this.selectNode = $(this.node).children('select')[0];
        }
        return this.selectNode;
    },

    getSelectedItems: function() {
        if (this.selectedValue != null) {
            return this.getChildrenByName(this.selectedValue);
        }

        return null;
    },

    select: function(value) {
        this.selectedValue = value;
        var selectedItems = this.getSelectedItems();
        for (var i = 0; i < selectedItems.length; i++) {
            selectedItems[i].node.style.display = "";
        }
        this.getChildrenButtons(value).show();
        this.click();
    },

    unselect: function() {
        var selectedItems = this.getSelectedItems();
        if (selectedItems) {
            for (var i = 0; i < selectedItems.length; i++) {
                selectedItems[i].node.style.display = "none";
            }
        }
        this.getChildrenButtons().hide();
        this.selectedValue = null;
    },

    validateSelect: function() {
        var selectedItems = this.getSelectedItems();
        if (selectedItems != null && selectedItems.length != 0) {
            this.raiseOnValidationSuccess([]);
            return 'exists';
        }
        if (this.getMode() == MItem.ViewMode || this.isOptional()) {
            this.raiseOnValidationSuccess([]);
            return 'isOptional';
        }

        this.raiseOnValidationError([new BM.NoItemSelectedValidationError(this)]);
        return 'hasError';
    },

    validate: function() {
        switch (this.validateSelect()) {
            case 'exists':
                var isValid = true;
                var selectedItems = this.getSelectedItems();
                for (var i = 0; i < selectedItems.length; i++) {
                    if (!selectedItems[i].validate()) {
                        isValid = false;
                    }
                }
                return isValid;
            case 'isOptional':
                return true;
            default:
                return false;
        }
    },

    focus: function() {
        this.raiseFocusing();
        return MForm.focus(this.getSelect());
    }
}

ChoiceItem.choose = function(select) {
    var options = select.options;
    var choiceItem = MItem.getByNode(select.parentNode);
    choiceItem.unselect();
    for (var i = 1; i < options.length; i++) {
        if (options[i].selected) {            
            choiceItem.select(options[i].value);
        }
    }
    $(select.parentNode).find("*[name = " + select.parentNode.id + "]").val(select.value);
}

ChoiceItem.registerClass("ChoiceItem", MItem);